package org.hxyjs.dao.impl;

import org.hxyjs.dao.BookskindDao;
import org.hxyjs.entity.Bookskind;
import org.hxyjs.entity.Bookskind;
import org.hxyjs.mapper.BookskindMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("bookskindDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class BookskindDaoImpl implements BookskindDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private BookskindMapper bookskindMapper;
    public List<Bookskind>  getAllBookskinds() throws Exception{
        return bookskindMapper.selectByExample(null);
    }

    @Override
    public Integer deleteBookskindByBkno(String bkno)  throws Exception{
        return bookskindMapper.deleteByPrimaryKey(bkno);
    }

    @Override
    public Integer updateBookskindByBkno(Bookskind bookskind)  throws Exception{
        return bookskindMapper.updateByPrimaryKey(bookskind);
    }

    @Override
    public Integer addBookskind(Bookskind bookskind)  throws Exception{
        bookskind.setBkno(initId());
        return bookskindMapper.insertSelective(bookskind);
    }

    @Override
    public List<Bookskind> getBookskindByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)bookskindMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return bookskindMapper.getBookskindByPage(map);
    }
    public String initId(){
        String maxBkno = bookskindMapper.getMaxBkno();
        System.out.println(GenNumTools.initId("",3,maxBkno));
        return GenNumTools.initId("",3,maxBkno);
    }

    @Override
    public List<Bookskind> selectBookskindWithbooks()  throws Exception{
        return bookskindMapper.selectBookskindWithbooks(null);
    }

}
